package com.carcontrol.rotate;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.carcontrol.rotate.base.BaseFragment;
import com.easy.carpanel.uart.uartJnit;


//灯光
public class Fragment_First extends BaseFragment {
    public static String TAG = "1"; //TODO 用于旋转，有待改进

    private FrameLayout v;
    private Context context;


    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;    //线程开关
    Thread thread;  //串口读取线程

    Object[] allParam = new Object[]{

//            new Object[]{"iv", new Rect(373*width/width0-width_d, 151*height/height0-height_d, 777*width/width0, 285*height/height0), R.mipmap.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "", 0},
//            new Object[]{"iv", new Rect(515*width/width0-width_d, 194*height/height0-height_d, 65*width/width0, 60*height/height0), R.mipmap.light, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "", 0},
//            new Object[]{"iv", new Rect(520*width/width0-width_d, 352*height/height0-height_d, 50*width/width0, 52*height/height0), R.mipmap.light_fw, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "", 0},

//            new Object[]{"iv", new Rect(596*width/width0-width_d, 198*height/height0-height_d, 58*width/width0, 52*height/height0), R.mipmap.light_level_0, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.mipmap.light_level_0},
//            new Object[]{"iv", new Rect((596+70)*width/width0-width_d, 198*height/height0-height_d, 51*width/width0, 52*height/height0), R.mipmap.light_level_1, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.mipmap.light_level_1},
//            new Object[]{"iv", new Rect((596+70+60)*width/width0-width_d, 198*height/height0-height_d, 51*width/width0, 52*height/height0), R.mipmap.light_level_2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.mipmap.light_level_2},
//            new Object[]{"iv", new Rect((596+70+60*2)*width/width0-width_d, 198*height/height0-height_d, 51*width/width0, 52*height/height0), R.mipmap.light_level_3, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.mipmap.light_level_3},
//            new Object[]{"iv", new Rect((596+70+60*3)*width/width0-width_d, 198*height/height0-height_d, 51*width/width0, 52*height/height0), R.mipmap.light_level_4, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.mipmap.light_level_4},
//            new Object[]{"iv", new Rect((596+70+60*4)*width/width0-width_d, 198*height/height0-height_d, 51*width/width0, 52*height/height0), R.mipmap.light_level_5, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "zm", R.mipmap.light_level_5},

            //照明灯
            new Object[]{"iv", new Rect(540*width/width0-width_d, 196*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.light_off, true, "D1E0", false, R.mipmap.light_on},
            //照明灯-
            new Object[]{"iv", new Rect(733*width/width0-width_d, 219*height/height0-height_d, 57*width/width0, 57*height/height0), R.mipmap.light_reduce_off, true, "D1D0", "zm", R.mipmap.light_reduce_on},
            //照明灯+
            new Object[]{"iv", new Rect(877*width/width0-width_d, 219*height/height0-height_d, 57*width/width0, 57*height/height0), R.mipmap.light_plus_off, true, "D1F0", "zm", R.mipmap.light_plus_on},


//            new Object[]{"iv", new Rect(596*width/width0-width_d, 350*height/height0-height_d, 58*width/width0, 52*height/height0), R.mipmap.light_fw_level_0, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_0},
//            new Object[]{"iv", new Rect((596+70)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_1, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_1},
//            new Object[]{"iv", new Rect((596+70+60)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_2},
//            new Object[]{"iv", new Rect((596+70+60*2)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_3, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_3},
//            new Object[]{"iv", new Rect((596+70+60*3)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_4, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_4},
//            new Object[]{"iv", new Rect((596+70+60*4)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_5, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_5},
//            new Object[]{"iv", new Rect((596+70+60*5)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_6, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_6},
//            new Object[]{"iv", new Rect((596+70+60*6)*width/width0-width_d, 350*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_7, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, "fw", R.mipmap.light_fw_level_7},

            //照明灯
            new Object[]{"iv", new Rect(540*width/width0-width_d, 330*height/height0-height_d, 103*width/width0, 103*height/height0), R.mipmap.light_fw_off, true, "B310", false, R.mipmap.light_fw_on},
            //氛围灯1
            new Object[]{"iv", new Rect(663*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_1_off, true, "B701", "fw", R.mipmap.light_fw_level_1_on},
            //氛围灯2
            new Object[]{"iv", new Rect(735*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_2_off, true, "B702", "fw", R.mipmap.light_fw_level_2_on},
            //氛围灯3
            new Object[]{"iv", new Rect(807*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_3_off, true, "B703", "fw", R.mipmap.light_fw_level_3_on},
            //氛围灯4
            new Object[]{"iv", new Rect(880*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_4_off, true, "B704", "fw", R.mipmap.light_fw_level_4_on},
            //氛围灯5
            new Object[]{"iv", new Rect(952*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_5_off, true, "B705", "fw", R.mipmap.light_fw_level_5_on},
            //氛围灯6
            new Object[]{"iv", new Rect(1024*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_6_off, true, "B706", "fw", R.mipmap.light_fw_level_6_on},
            //氛围灯7
            new Object[]{"iv", new Rect(1096*width/width0-width_d, 351*height/height0-height_d, 56*width/width0, 56*height/height0), R.mipmap.light_fw_level_7_off, true, "B707", "fw", R.mipmap.light_fw_level_7_on},

            //照明灯文字
            new Object[]{"iv", new Rect(398*width/width0-width_d, 232*height/height0-height_d, 108*width/width0, 31*height/height0), R.mipmap.txt_light, false, "", "fw", R.mipmap.light_fw_level_6_on},
            //氛围灯文字
            new Object[]{"iv", new Rect(398*width/width0-width_d, 363*height/height0-height_d, 108*width/width0, 32*height/height0), R.mipmap.txt_light_fw, false, "", "fw", R.mipmap.light_fw_level_7_on},

//            new Object[]{"tv", new Rect(398*width/width0-width_d, 208*height/height0-height_d, 0, 0), "照明灯", 18, Color.WHITE},
//            new Object[]{"tv", new Rect(398*width/width0-width_d, 357*height/height0-height_d, 0, 0), "氛围灯", 18, Color.WHITE},
    };

    private ImageView iv_light; //照明灯
    private ImageView iv_light_fw; //照明灯
    private ImageView iv_light_fw_level_1;  //氛围灯
    private ImageView iv_light_fw_level_2;
    private ImageView iv_light_fw_level_3;
    private ImageView iv_light_fw_level_4;
    private ImageView iv_light_fw_level_5;
    private ImageView iv_light_fw_level_6;
    private ImageView iv_light_fw_level_7;

    private boolean isLight = false; //照明灯是否关闭
    private boolean isLight_fw = false; //氛围灯是否关闭

    Object[] lightObj;
    Object[] lightFwObj;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());
//        v = (FrameLayout) inflater.inflate(R.layout.fragment_first, null);


        context = v.getContext();

        Log.e("fragment_First===0", v+"==="+container);



        initView();

//        isThread = true;
//        loopRead();


        return v;
    }

    protected void doAction1(String myDevice, String myStatus){

        Log.e("f1===doAction1", myDevice+"==="+myStatus);

        if(myDevice.equals("d001")){
            if(myStatus.equals("0001")){  //TODO
                iv_light.setImageResource(R.mipmap.light_on);
                lightObj[5] = true;
            }else{
                iv_light.setImageResource(R.mipmap.light_off);
                lightObj[5] = false;
            }
        }else if(myDevice.equals("b003")){
            if(myStatus.equals("0001")){  //TODO
                iv_light_fw.setImageResource(R.mipmap.light_fw_on);
                lightFwObj[5] = true;
            }else{
                iv_light_fw.setImageResource(R.mipmap.light_fw_off);
                lightFwObj[5] = false;
            }
        }
    }

    @Override
    public void onDestroy() {
        Log.e("f1===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);



            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                int imgBack = (int)obj[6];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                if(imgSrc==R.mipmap.light_off){
                    lightObj = obj;
                    iv_light = iv;

                    boolean isOpen = (boolean)obj[5];

                    Log.e("iv===isOpen", "==="+isOpen);

                    if(isOpen){
                        iv.setImageResource(R.mipmap.light_on);
                    }else{
                        iv.setImageResource(R.mipmap.light_off);
                    }


                }else if(imgSrc==R.mipmap.light_fw_off){
                    lightFwObj = obj;
                    iv_light_fw = iv;

                    boolean isOpen = (boolean)obj[5];

                    Log.e("iv===isOpen_fw", "==="+isOpen);

                    if(isOpen){
                        iv.setImageResource(R.mipmap.light_fw_on);
                    }else{
                        iv.setImageResource(R.mipmap.light_fw_off);
                    }


                }else if(imgSrc==R.mipmap.light_fw_level_1_off){
                    iv_light_fw_level_1 = iv;
                }else if(imgSrc==R.mipmap.light_fw_level_2_off){
                    iv_light_fw_level_2 = iv;
                }else if(imgSrc==R.mipmap.light_fw_level_3_off){
                    iv_light_fw_level_3 = iv;
                }else if(imgSrc==R.mipmap.light_fw_level_4_off){
                    iv_light_fw_level_4 = iv;
                }else if(imgSrc==R.mipmap.light_fw_level_5_off){
                    iv_light_fw_level_5 = iv;
                }else if(imgSrc==R.mipmap.light_fw_level_6_off){
                    iv_light_fw_level_6 = iv;
                }else if(imgSrc==R.mipmap.light_fw_level_7_off){
                    iv_light_fw_level_4 = iv;
                }


                v.addView(iv, params);


                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===first", event.getAction()+"==="+MainActivity.curPos);

                            if(MainActivity.curPos!=0) return false;

                            int imgId = (int)obj[2];
                            int imgId2 = (int)obj[6];
                            String cmd = (String)obj[4];

                            if(event.getAction()==1){
                                if(imgId!=R.mipmap.light_off && imgId!=R.mipmap.light_fw_off){
                                    iv.setImageResource(imgId);   //TODO
                                }


//                                cmdOne = "D1E0" + "0030";
//                                String allCmdx = cmdheader;
//                                String tmpTmp = "D1E0" + "0030";
//                                allCmdx = allCmdx.concat(CRC16.expandStr(tmpTmp));
//                                StringBuffer allCmd = CRC16.crc16x(allCmdx);
//                                String cmd = allCmd.substring(0,22);
//
//                                Log.e("onTouch===1", "==="+cmd);

//                                  write(bytes); //TODO


//                                if(imgId==R.mipmap.light){
//                                    iv.setImageResource(R.mipmap.light_0);
//                                    obj[2] = R.mipmap.light_0;
//                                }else if(imgId==R.mipmap.light_0){
//                                    iv.setImageResource(R.mipmap.light);
//                                    obj[2] = R.mipmap.light;
//                                }else if(imgId==R.mipmap.light_fw){
//                                    iv.setImageResource(R.mipmap.light_fw_0);
//                                    obj[2] = R.mipmap.light_fw_0;
//                                }else if(imgId==R.mipmap.light_fw_0){
//                                    iv.setImageResource(R.mipmap.light_fw);
//                                    obj[2] = R.mipmap.light_fw;
//                                }else if(imgId==R.mipmap.light_level_0){
//                                    iv.setImageResource(R.mipmap.light_level_0f);
//                                    obj[2] = R.mipmap.light_level_0f;
//                                }else if(imgId==R.mipmap.light_level_0f){
//                                    iv.setImageResource(R.mipmap.light_level_0);
//                                    obj[2] = R.mipmap.light_level_0;
//                                }else if(imgId==R.mipmap.light_fw_level_0){
//                                    iv.setImageResource(R.mipmap.light_fw_level_0f);
//                                    obj[2] = R.mipmap.light_fw_level_0f;
//                                }else if(imgId==R.mipmap.light_fw_level_0f){
//                                    iv.setImageResource(R.mipmap.light_fw_level_0);
//                                    obj[2] = R.mipmap.light_fw_level_0;
//                                }else{
//                                    iv.setImageResource(imgId);
//                                }

//                                if(imgId==R.mipmap.light_off){
////                                    iv_light.setImageResource(R.mipmap.light_off);
//                                    uartJnit.getInstance().sendCmds(toCmd(cmd)); //TODO   "5AA50883D1E0000000903A"
//                                }else if(imgId==R.mipmap.light_reduce_off){
//                                    iv.setImageResource(R.mipmap.light_reduce_off);
////                                    uartJnit.getInstance().sendCmds(toCmd("D1D0"));
//                                }else if(imgId==R.mipmap.light_plus_off){
//                                    iv.setImageResource(R.mipmap.light_plus_off);
////                                    uartJnit.getInstance().sendCmds(toCmd("D1F0"));
//                                }
//                                else if(imgId==R.mipmap.light_fw_level_1_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B701"));
//                                }else if(imgId==R.mipmap.light_fw_level_2_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B702"));
//                                }else if(imgId==R.mipmap.light_fw_level_3_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B703"));
//                                }else if(imgId==R.mipmap.light_fw_level_4_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B704"));
//                                }else if(imgId==R.mipmap.light_fw_level_5_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B705"));
//                                }else if(imgId==R.mipmap.light_fw_level_6_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B706"));
//                                }else if(imgId==R.mipmap.light_fw_level_7_off){
//                                    uartJnit.getInstance().sendCmds(toCmd("B707"));
//                                }

//                                uartJnit.getInstance().sendCmds(toCmd(cmd));
                                send(cmd);

                            }else{
                                if(imgId!=R.mipmap.light_off && imgId!=R.mipmap.light_fw_off){
                                    iv.setImageResource(imgId2);   //TODO
                                }

//                                if(imgId==R.mipmap.light_off){
////                                    iv.setImageResource(R.mipmap.light_on);
//                                }else if(imgId==R.mipmap.light_plus_off){
//                                    iv.setImageResource(R.mipmap.light_plus_on);
//                                }else if(imgId==R.mipmap.light_reduce_off){
//                                    iv.setImageResource(R.mipmap.light_reduce_on);
//                                }
//
//                                else if(imgId==R.mipmap.light_fw_level_1_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_1_on);
//                                }else if(imgId==R.mipmap.light_fw_level_2_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_2_on);
//                                }else if(imgId==R.mipmap.light_fw_level_3_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_3_on);
//                                }else if(imgId==R.mipmap.light_fw_level_4_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_4_on);
//                                }else if(imgId==R.mipmap.light_fw_level_5_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_5_on);
//                                }else if(imgId==R.mipmap.light_fw_level_6_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_6_on);
//                                }else if(imgId==R.mipmap.light_fw_level_7_off){
//                                    iv.setImageResource(R.mipmap.light_fw_level_7_on);
//                                }


                            }



                            return true;
                        }
                    });

//                    iv.setOnClickListener(new View.OnClickListener() {
//
//                        @Override
//                        public void onClick(View v) {
//
//                            int imgId = (int)obj[2];
//                            String type2 = (String)obj[5];
//                            byte[] bytes = (byte[])obj[4];
//
//                            Log.e("fragment_First===iv", imgId+"==="+bytes.length+"==="+bytes);//2131165321 2131165322
//                        }
//                    });
                }

            }

        }


    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    if(MainActivity.curPos==0){
                        m_myHandler.sendEmptyMessage(1);
                    }

                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO 串口获取车速

                    Log.e("m_myHandler===", "===");

//                    readFeedback();

                default:
                    break;
            }
            return false;
        }
    });




    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}

